package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OrderServiceItemDto;
import com.csun.cmny.provider.model.dto.attachment.OptAttachmentRespDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcOrderServiceItemContentService;
import com.csun.cmny.provider.service.OmcOrderServiceItemService;
import com.csun.cmny.provider.service.OpcRpcService;
import com.csun.cmny.provider.validator.OmcOrderServiceItemAddValidator;
import com.csun.cmny.provider.vo.OrderServiceItemVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/orderServiceItem")
public class OmcOrderServiceItemController extends BaseController {

    @Resource
    CmnyMessageSource cmnyMessageSource;

    @Resource
    OmcOrderServiceItemService omcOrderServiceItemService;

    @Resource
    OmcOrderServiceItemContentService omcOrderServiceItemContentService;

    @Resource
    OpcRpcService opcRpcService;

    /**
     * 订单项添加
     *
     * @param: [orderItemDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addOrderServiceItem(@Validated(OmcOrderServiceItemAddValidator.class) @RequestBody OrderServiceItemDto orderServiceItemDto,
                           BindingResult br) {

        Integer result = omcOrderServiceItemService.saveOrderServiceItem(orderServiceItemDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     *
     * @param: [OrderId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListByOrderId/{orderId}")
    public Wrapper queryListByOrderId(@PathVariable Long orderId) {

        // 校验Id是否为空
        Preconditions.checkArgument(orderId != null, cmnyMessageSource
                .getMessage("order.id.null", null));
        List<OrderServiceItemVo> orderServiceItemVos = omcOrderServiceItemService.selectByOrderId(orderId);
        //return WrapMapper.ok(orderServiceItemVos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),orderServiceItemVos);
    }

   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryAllByOrderId/{orderId}")
    public Wrapper queryAllByOrderId(@PathVariable Long orderId) {

        // 校验Id是否为空
        Preconditions.checkArgument(orderId != null, cmnyMessageSource
                .getMessage("order.id.null", null));
        List<OrderServiceItemVo> orderServiceItemVos = omcOrderServiceItemService.selectByOrderId(orderId);
        List<Long> orderServiceItemUrls = new ArrayList<>();
        orderServiceItemVos = omcOrderServiceItemContentService.setContents(orderServiceItemVos);
        for(OrderServiceItemVo vo : orderServiceItemVos){
            if( !vo.getUrl().equals("") && vo.getUrl() != null){
                orderServiceItemUrls.add(Long.parseLong(vo.getUrl()));
            }
        }
        List<OptAttachmentRespDto> optAttachmentRespDtos = opcRpcService.queryAttachmentById(orderServiceItemUrls);
        for(int i = 0,j = optAttachmentRespDtos.size()-1;i<=j;i++){
            for(int k = 0 , m = orderServiceItemVos.size()-1;k<=m;k++){
                if( !orderServiceItemVos.get(k).getUrl().equals("") && orderServiceItemVos.get(k).getUrl()!=null){
                    if(optAttachmentRespDtos.get(i).getId().toString().equals(orderServiceItemVos.get(k).getUrl())){
                        orderServiceItemVos.get(k).setUrl("http://51cs8.com/" + optAttachmentRespDtos.get(i).getPath() + optAttachmentRespDtos.get(i).getName());
                    }
                }
            }
        }
        //return WrapMapper.ok(orderServiceItemVos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),orderServiceItemVos);
    }

}
